package com.ssy.bus.controller;

import com.ssy.bus.pojo.Prescription;
import com.ssy.bus.service.IPrescriptionService;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/prescription")
public class PrescriptionController {

    @Autowired
    private IPrescriptionService prescriptionService;

    /**
     * 添加药用处方
     *
     * @param prescription prescription
     * @return data
     */
    @PostMapping("/addDrugPrescription")
    public Result addDrugPrescription(Prescription prescription) {
        return this.prescriptionService.addDrugPrescription(prescription);
    }

    /**
     * 添加检查处方
     *
     * @param prescription prescription
     * @return data
     */
    @PostMapping("/addInspectPrescription")
    public Result addInspectPrescription(Prescription prescription) {
        return this.prescriptionService.addInspectPrescription(prescription);
    }

    /**
     * 加载药用处方的数据表格
     *
     * @param outpatientId 挂号单ID
     * @return data
     */
    @GetMapping("/loadDrugInfoTable/{outpatientId}")
    public Result loadDrugInfoTable(@PathVariable String outpatientId) {
        return this.prescriptionService.loadDrugInfoTable(outpatientId);
    }

    /**
     * 加载检查项处方的数据表格
     *
     * @param outpatientId 挂号单ID
     * @return data
     */
    @GetMapping("/loadInspectTable/{outpatientId}")
    public Result loadInspectTable(@PathVariable String outpatientId) {
        return this.prescriptionService.loadInspectTable(outpatientId);
    }

    /**
     * 删除药用处方
     *
     * @param outpatientId 挂号单ID
     * @param id           药品ID
     * @return data
     */
    @DeleteMapping("/deletePrescription/{outpatientId}/{id}")
    public Result deletePrescription(@PathVariable String outpatientId, @PathVariable Integer id) {
        return this.prescriptionService.deletePrescription(outpatientId, id, 1);
    }

    /**
     * 删除检查处方
     *
     * @param outpatientId 挂号单ID
     * @param id           检查项ID
     * @return data
     */
    @DeleteMapping("/deletePrescriptionByInspectId/{outpatientId}/{id}")
    public Result deletePrescriptionByInspectId(@PathVariable String outpatientId, @PathVariable Integer id) {
        return this.prescriptionService.deletePrescription(outpatientId, id, 2);
    }

    /**
     * 加载药用处方的总价
     *
     * @param outpatientId 挂号单ID
     * @return money
     */
    @GetMapping("/loadDrugMoney/{outpatientId}")
    public Result loadDrugMoney(@PathVariable String outpatientId) {
        List<Prescription> prescriptions = (List<Prescription>) this.prescriptionService.loadDrugInfoTable(outpatientId).getData();
        BigDecimal money = new BigDecimal(0);
        for (Prescription prescription : prescriptions) {
            money = money.add(prescription.getMoney());
        }
        return new Result(money.toString());
    }

    /**
     * 加载检查项处方的总价
     *
     * @param outpatientId 挂号单ID
     * @return money
     */
    @GetMapping("/loadInspectMoney/{outpatientId}")
    public Result loadInspectMoney(@PathVariable String outpatientId) {
        List<Prescription> prescriptions = (List<Prescription>) this.prescriptionService.loadInspectTable(outpatientId).getData();
        BigDecimal money = new BigDecimal(0);
        for (Prescription prescription : prescriptions) {
            money = money.add(prescription.getMoney());
        }
        return new Result(money.toString());
    }


    /**
     * 修改药用处方的数量
     *
     * @param outpatientId 挂号单ID
     * @param drugId       药品ID
     * @param number       修改后的数量
     * @return data
     */
    @PostMapping("/updateDrugTableNumber/{outpatientId}/{drugId}/{number}")
    public Result updateDrugTableNumber(@PathVariable String outpatientId, @PathVariable Integer drugId, @PathVariable Integer number) {
        return this.prescriptionService.updatePrescriptionByDrug(outpatientId, drugId, number);
    }

}
